package cn.fldong.employee.servlet;

import cn.fldong.employee.dao.EmployeeDao;
import cn.fldong.employee.entity.EmployeeEntity;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

@WebServlet("/updateEmployee")
public class UpdateInfoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        EmployeeEntity employee = new EmployeeEntity();
        // 员工信息表必须包含字段：工号ID、姓名、性别、年龄、部门、职位、入职日期、工资
        int id = Integer.parseInt(req.getParameter("empID"));
        employee.setEmpID(id);
        employee.setEmpName(req.getParameter("empName"));
        employee.setEmpGender(req.getParameter("empGender"));
        int empAge = Integer.parseInt(req.getParameter("empAge"));
        employee.setEmpAge(empAge);
        employee.setEmpDepartment(req.getParameter("empDepartment"));
        employee.setEmpPosition(req.getParameter("empPosition"));
        // 入职日期格式为yyyy-MM-dd
        String empHireDate = req.getParameter("empHireDate");
        employee.setEmpHireDate(empHireDate);
        // 工资字段为BigDecimal类型
        BigDecimal empSalary = new BigDecimal(req.getParameter("empSalary"));
        employee.setEmpSalary(BigDecimal.valueOf(Double.parseDouble(req.getParameter("empSalary"))));
        // 保存员工信息到数据库
        EmployeeDao dao=new EmployeeDao();
        String message=null;

        if(dao.updateEmployee(employee)){
            message = "员工信息更新成功！";
        }else{
            message = "员工信息更新失败！";
        }

        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<html><body>");
        out.println("<h1>" + message + "</h1>");
        out.println("<h2><a href='update.jsp'>"+"返回"+"</a></h2>");
        out.println("</body></html>");
        out.close();
    }
}
